package edu.ccut.saturn.studio.naviagtor.links;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.ide.ResourceUtil;
import org.eclipse.ui.navigator.ILinkHelper;
import org.eclipse.ui.part.FileEditorInput;

public class SaturnLinkHelper implements ILinkHelper {

	public void activateEditor(IWorkbenchPage page,
			IStructuredSelection selection) {
		
		if (selection == null || selection.isEmpty()) {
			return;
		}
		
		if (selection.getFirstElement() instanceof IFile) {
			IEditorInput fileInput = new FileEditorInput((IFile) selection.getFirstElement());
			IEditorPart editor = page.findEditor(fileInput);
			
			if (editor != null) {
				page.bringToTop(editor);
			}
		}
	}

	public IStructuredSelection findSelection(IEditorInput anInput) {
		IFile file = ResourceUtil.getFile(anInput);
		if (file != null) {
			return new StructuredSelection(file);
		}

		return StructuredSelection.EMPTY;
	}
}
